package com.glzn.springboot03.mapper;

import com.glzn.springboot03.entity.Para;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Administrator
 */
@Mapper
@Repository
public interface ParaMapper {
	/**
	 * 查询所有定值
	 * @param deviceId
	 * @return
	 */
	@Select("SELECT * FROM para WHERE device_id=#{deviceId}")
	List<Para> findAll(Integer deviceId);

	/**
	 * 根据定值类型查询定值
	 * @param areaNum
	 * @param deviceId
	 * @return
	 */
	@Select("SELECT * FROM para WHERE area_num=#{areaNum} AND device_id=#{deviceId}")
	List<Para> findByAreaNum(int areaNum, Integer deviceId);

	/**
	 * 根据定值类型和通道号查询定值
	 * @param type
	 * @param areaNum
	 * @param deviceId
	 * @return
	 */
	@Select("SELECT * FROM para WHERE type=#{type} AND area_num=#{areaNum} AND device_id=#{deviceId}")
	List<Para> findByTypeAndAreaNum(String type, int areaNum, Integer deviceId);

	/**
	 * 根据通道号查询事件
	 * @param paraNum
	 * @param deviceId
	 * @return
	 */
	@Select("SELECT * FROM para WHERE para_num=#{paraNum} AND device_id=#{deviceId}")
	List<Para> findByParaNum(int paraNum, Integer deviceId);

	/**
	 * 插入
	 * @param para
	 * @return
	 */
	@SelectKey(statement = "SELECT MAX(id)+1 FROM para", keyProperty = "id", before = true, resultType = int.class)
	@Insert("INSERT INTO para(id,para_num,area_num,format,type,`name`,`value`,`max_value`,min_value,int_bit_num,dec_bit_num,unit,`option`,device_id) VALUES(#{id},#{paraNum},#{areaNum},#{format},#{type},#{name},#{value},#{maxValue},#{minValue},#{intBitNum},#{decBitNum},#{unit},#{option},#{deviceId})")
	Boolean insert(Para para);

	/**
	 * 删除所有定值
	 * @param deviceId
	 * @return
	 */
	@Delete("DELETE FROM para WHERE device_id=#{deviceId}")
	boolean deleteAll(Integer deviceId);
}
